import { Injectable } from "@angular/core";
import { Observable, of, throwError } from "rxjs";
import {
  HttpClient,
  HttpHeaders,
  HttpErrorResponse,
  HttpParams
} from "@angular/common/http";
import { catchError, tap, map, take } from "rxjs/operators";

import {
  ILoginRequest,
  ILoginResponse,
  IEnrollRequest,
  IEnrollResponse
} from "@/user/store/models/user";

const httpOptions: any = {
  headers: new HttpHeaders({ "Content-Type": "application/json" })
};
const apiUrl = "/sliderCode";

@Injectable({
  providedIn: "root"
})
export class SliderCodeService {
  constructor(private http: HttpClient) {}

  private handleError(error: HttpErrorResponse) {
    if (error.error instanceof ErrorEvent) {
      // A client-side or network error occurred. Handle it accordingly.
      console.error("An error occurred:", error.error.message);
    } else {
      // The backend returned an unsuccessful response code.
      // The response body may contain clues as to what went wrong,
      console.error(
        `Backend returned code ${error.status}, ` + `body was: ${error.error}`
      );
    }
    // return an observable with a user-facing error message
    return throwError("Something bad happened; please try again later.");
  }

  select(): Observable<any> {
    return this.http
      .get(apiUrl, httpOptions)
      .pipe(catchError(this.handleError));
  }

  find(id: string): Observable<any> {
    const url = `${apiUrl}/${id}`;
    return this.http.get(url, httpOptions).pipe(catchError(this.handleError));
  }

  add(data): Observable<any> {
    return this.http
      .post(apiUrl, data, httpOptions)
      .pipe(catchError(this.handleError));
  }

  save(id: string, data): Observable<any> {
    delete data.id;
    const url = `${apiUrl}/${id}`;
    return this.http
      .put(url, data, httpOptions)
      .pipe(catchError(this.handleError));
  }

  del(id: string): Observable<{}> {
    const url = `${apiUrl}/${id}`;
    return this.http
      .delete(url, httpOptions)
      .pipe(catchError(this.handleError));
  }

  /**
   * 注册滑动验证码
   *
   * @param {ISliderCodeRequest} req
   * @returns
   * @memberof SliderCodeService
   */
  register(req: ISliderCodeRegisterRequest) {
    const url = `${apiUrl}/register-${req.type}`;
    return this.http.get(url, httpOptions).pipe(catchError(this.handleError));
  }

  /**
   * 验证滑动验证码
   *
   * @param {ISliderCodeRequest} req
   * @returns
   * @memberof SliderCodeService
   */
  validate(req: ISliderCodeValidateRequest) {
    const url = `${apiUrl}/validate-${req.type}`;
    return this.http.get(url, httpOptions).pipe(catchError(this.handleError));
  }
}

/**
 * 滑动验证码 注册 请求参数
 *
 * @export
 * @interface ISliderCodeRequest
 */
export interface ISliderCodeRegisterRequest {
  type: undefined | null | "fullpage" | "click" | "slide";
}

/**
 * 滑动验证码 验证 请求参数
 *
 * @export
 * @interface ISliderCodeValidateRequest
 */
export interface ISliderCodeValidateRequest {
  type: undefined | null | "fullpage" | "click" | "slide";
}
